package com.um.jdy.platform.shop.controller;


import com.um.jdy.platform.order.entity.vo.OrderOverviewVO;
import com.um.jdy.platform.order.manager.OrderOverviewManager;
import com.um.jdy.platform.shop.entity.vo.ShopOverviewItemVO;
import com.um.jdy.platform.shop.entity.vo.ShopOverviewVO;
import com.um.jdy.platform.shop.manager.ShopOverviewManager;
import com.um.springboot.starter.annotation.ApiPermission;
import com.um.springboot.starter.annotation.TokenRequired;
import com.um.springboot.starter.orm.controller.BaseController;
import com.um.springboot.starter.orm.entity.Response;
import com.um.springboot.starter.service.CacheService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.TimeUnit;


/**
 * 充电订单
 *
 * @author cs
 * 日期 2023/6/7
 * 版权所有 广州优匠科技有限公司
 */
@RestController
@RequestMapping("/api/v1/cms/shop/overview")
@Api(tags = "商城订单概览")
@ApiPermission("shop_overview")
@TokenRequired
@Validated
public class ShopOverviewController extends BaseController<ShopOverviewManager> {

    @Autowired
    RedisTemplate redisTemplate;

    private static final String CACHE_KEY = "shopOverview";

    @ApiPermission("data")
    @ApiOperation("订单概览")
    @GetMapping({"data"})
    public Response<ShopOverviewVO> getOverviewData(){
        ShopOverviewVO data = (ShopOverviewVO)redisTemplate.opsForValue().get(CACHE_KEY+"-"+manager.getComId());
        if (data == null) {
            data = manager.getOverviewData();
            redisTemplate.opsForValue().set(CACHE_KEY+"-"+manager.getComId(), data, 1L, TimeUnit.MINUTES);
        }
        return Response.success(data);
    }

    @ApiPermission("amount_list")
    @ApiOperation("销售额列表")
    @GetMapping({"{dateType}/amount_list"})
    public Response<List<ShopOverviewItemVO>> getSaleAmountList(@ApiParam("类型：0-今日 1-近7天 2-近30天") @PathVariable String dateType){
        return Response.success(manager.getSaleCharts(dateType));
    }

    @ApiPermission("order_list")
    @ApiOperation("订单列表")
    @GetMapping({"{dateType}/order_list"})
    public Response<List<ShopOverviewItemVO>> getSaleOrderList(@ApiParam("类型：0-今日 1-近7天 2-近30天") @PathVariable String dateType){
        return Response.success(manager.getSaleCharts(dateType));
    }

}
